package com.xydtech.ecard.ecard.controller;

import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardModuleInfo;
import com.xydtech.ecard.config.service.IEcardModuleInfoService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 电子名片-模块功能开关配置
 *
 * @author yuzhiguo
 * @date 2024-10-22 下午3:04
 */
@Controller
@Api(tags = "电子名片-模块功能开关配置")
@RequestMapping("/ecardSaasApi/config/module")
public class EcardApiModuleInfoController extends EcardApiBaseController {

    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private IEcardModuleInfoService ecardModuleInfoService;

    /**
     * 电子名片-电子名片-获取模块功能开关配置列表
     *
     * @author yuzhiguo
     * @date 2024/10/22 下午3:07
     */
    @PassToken
    @ApiOperation(value = "电子名片-电子名片-获取模块功能开关配置列表", notes = "电子名片-电子名片-获取模块功能开关配置列表")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @GetMapping("/findModuleList")
    @ResponseBody
    @RsaRequest(param = false)
    public ResponseModel findModuleList() {
        String salt = getSalt();//盐值
        String currentUserId = getCurrentUserId();
        EcardModuleInfo moduleInfo = new EcardModuleInfo();
        if (StringUtils.objIsNullOrEmpty(currentUserId)){
            //未登录返回所有的功能配置列表
            moduleInfo.setDeptId(100L);
            moduleInfo.setDictDeptId(100L);
        } else {
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            String appId = wechatApplet.getAppid();
            if (StringUtils.isEmpty(appId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
            }
            //当前小程序对应的法人机构ID
            Long parentDeptId = wechatApplet.getDeptId();
            moduleInfo.setDeptId(parentDeptId);
            moduleInfo.setDictDeptId(parentDeptId);
            moduleInfo.setState(1);//启用
        }
        List<EcardModuleInfo> ecardModuleInfoList = ecardModuleInfoService.selectEcardModuleInfoList(moduleInfo);
        return ResponseModel.okToJSONArray(ecardModuleInfoList);
    }

}
